﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Core;

namespace SharedGenomics.Workbench.Views.WindowsForms
{
    public partial class View : UserControl, IView
    {
        public View()
        {
            InitializeComponent();
        }

        private void UserControl_Load(object sender, EventArgs e)
        {
            //detach handler
            this.Load -= this.UserControl_Load;

            //search through each control on this control (top level only) - if any controls are marked with a HelpAttribute then there should be an associated
            //help topic for the control. This control should add a help button next to the control by:
            // - removing the control from the form
            // - adding a FlowLayoutPanel to the form in the same location
            // - adding the control and a help button to the flow panel
            var helpControls = from prop in this.GetType().GetProperties()
                               let helpAtts = prop.GetCustomAttributes(typeof(HelpAttribute), true)
                               where helpAtts.Length > 0
                               select new { Control = (Control)prop.GetValue(this, null), HelpAttribute = (HelpAttribute)helpAtts[0] };

            foreach (var hc in helpControls)
            {
                Control c = hc.Control;
                Point loc = c.Location;
                //loc.X -= 30;
                Size cSize = c.Size;
                this.Controls.Remove(c);
                FlowLayoutPanel flowPanel = new FlowLayoutPanel() { Height = c.Height + 2 };
                flowPanel.Location = loc;

                var icon = new HelpIcon(hc.HelpAttribute.Topic);
                icon.Click += new EventHandler(this.Icon_Click);

                flowPanel.Controls.Add(c);
                flowPanel.Controls.Add(icon);

                this.Controls.Add(flowPanel);
            }

            this.ViewLoaded.Raise(this);
        }

        private void Icon_Click(object sender, EventArgs e)
        {
            HelpIcon icon = (HelpIcon)sender;
            this.HelpRequired.Raise(this, new HelpRequiredEventArgs(icon.HelpTopic));
        }

        protected virtual IEnumerable<Binding> ValidatedDataBindings
        {
            get { return Enumerable.Empty<Binding>(); }
        }

        public virtual IValidationErrorCollection ValidationErrors
        {
            set
            {
                this.errorProvider.Clear();

                var controlMessages = value.Join(   this.ValidatedDataBindings,
                                                    e => (ObjectPropertyWrapper)e, b => new ObjectPropertyWrapper(b.BindingManagerBase.Current, b.BindingMemberInfo.BindingMember),
                                                    (err, binding) => new { Control = binding.Control, ErrorMessage = err.Message });

                //get all bindings with validation errors and the associated error message
                foreach(var cm in controlMessages)
                {
                    this.errorProvider.SetError(cm.Control, cm.ErrorMessage);
                }
            }
        }

        public virtual void DisplayError(string message)
        {
            MessageBox.Show(message);
        }

        public virtual string Title
        {
            get { return "View"; }
        }

        public event EventHandler<HelpRequiredEventArgs> HelpRequired;
        public event EventHandler ViewLoaded;
    }
}
